package com.yange.accountbook.ui

import android.widget.RadioButton
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.google.gson.Gson
import com.yange.accountbook.R
import com.yange.accountbook.databinding.ActivityMainBinding
import com.yange.accountbook.entity.UserInfoEntity
import com.yange.accountbook.net.Api
import com.yange.accountbook.net.BaseResponse
import com.yange.accountbook.net.api.RetrofitClient
import com.yange.accountbook.net.observable.RecObserver
import com.yange.accountbook.utils.ManualRadioGroup
import com.yange.accountbook.utils.UserDao
import com.yange.accountbook.utils.showToast

class MainActivity : BaseActivity<ActivityMainBinding>() {
    override fun getViewBinding() = ActivityMainBinding.inflate(layoutInflater)

    override fun init() {
        refreshUserInfo()

        binding.mainRadiogroup.onChildRadioButtonClickedListener =
            object : ManualRadioGroup.OnChildRadioButtonClickedListener {
                override fun onRadioButtonClickedWhenCheckedNone(button: RadioButton?) {
                    when (button?.id) {
                        R.id.rb_home -> replaceFragment(
                            R.id.main_ll,
                            mFragmentList[0],
                            supportFragmentManager
                        )
                        R.id.rb_enterprise -> replaceFragment(
                            R.id.main_ll,
                            mFragmentList[1],
                            supportFragmentManager
                        )
                        R.id.rb_message -> replaceFragment(
                            R.id.main_ll,
                            mFragmentList[2],
                            supportFragmentManager
                        )
                        R.id.rb_mine -> replaceFragment(
                            R.id.main_ll,
                            mFragmentList[3],
                            supportFragmentManager
                        )
                    }
                    button?.isChecked = true
                }

                override fun onRadioButtonCheckedClicked(button: RadioButton?) {
                    when (button?.id) {
                        R.id.rb_home -> replaceFragment(
                            R.id.main_ll,
                            mFragmentList[0],
                            supportFragmentManager
                        )
                        R.id.rb_enterprise -> replaceFragment(
                            R.id.main_ll,
                            mFragmentList[1],
                            supportFragmentManager
                        )
                        R.id.rb_message -> replaceFragment(
                            R.id.main_ll,
                            mFragmentList[2],
                            supportFragmentManager
                        )
                        R.id.rb_mine -> replaceFragment(
                            R.id.main_ll,
                            mFragmentList[3],
                            supportFragmentManager
                        )
                    }
                }

                override fun onRadioButtonDifferentFromCheckedClicked(
                    clickedRadioButton: RadioButton?,
                    checkedRadioButton: RadioButton?
                ) {
                    when (clickedRadioButton?.id) {
                        R.id.rb_home -> {
                            replaceFragment(R.id.main_ll, mFragmentList[0], supportFragmentManager)
                            clickedRadioButton.isChecked = true
                        }
                        R.id.rb_enterprise -> {
                            replaceFragment(R.id.main_ll, mFragmentList[1], supportFragmentManager)
                            clickedRadioButton.isChecked = true
                        }
                        R.id.rb_message -> {
                            replaceFragment(R.id.main_ll, mFragmentList[2], supportFragmentManager)
                            clickedRadioButton.isChecked = true
                        }
                        R.id.rb_mine -> {
                            replaceFragment(R.id.main_ll, mFragmentList[3], supportFragmentManager)
                            clickedRadioButton.isChecked = true
                        }
                    }

                }
            }
        replaceFragment(R.id.main_ll, mFragmentList[0], supportFragmentManager)
    }

    fun replaceFragment(
        contentLayoutId: Int,
        fragment: Fragment,
        fragmentManager: FragmentManager
    ) {
        val transaction = fragmentManager.beginTransaction()
        val fragments = fragmentManager.fragments
        for (mFragment in fragments) {
            transaction.hide(mFragment)
        }
        if (!fragment.isAdded) {
            transaction.add(contentLayoutId, fragment).commitAllowingStateLoss()
        } else {
            transaction.show(fragment).commitAllowingStateLoss()
        }
    }

    private var mFragmentList: MutableList<Fragment> = mutableListOf(
        BookFragment(),
        OrderListFragment(),
        ReportFragment(),
        MineFragment()
    )

    private fun refreshUserInfo() {
        RetrofitClient.getInstance().invokeGet(this , Api.USER_INFO)
            .subscribe(object : RecObserver<BaseResponse<UserInfoEntity>>(this, true, false) {
                override fun onSuccess(t: BaseResponse<UserInfoEntity>?) {
                    if (t != null) {
                        UserDao.info =Gson().toJson(t.data)
                    }
                }

                override fun onFail(msg: String, code: Int) {
                    showToast(msg)
                }
            })
    }
}